package com.jxdinfo.hussar.example.mes.md.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jxdinfo.hussar.example.common.annotation.Log;
import com.jxdinfo.hussar.example.common.core.controller.BaseController;
import com.jxdinfo.hussar.example.common.core.domain.AjaxResult;
import com.jxdinfo.hussar.example.common.core.page.TableDataInfo;
import com.jxdinfo.hussar.example.common.enums.BusinessType;
import com.jxdinfo.hussar.example.common.utils.StringUtils;
import com.jxdinfo.hussar.example.common.utils.poi.ExcelUtil;
import com.jxdinfo.hussar.example.mes.md.domain.MdWorkshop;
import com.jxdinfo.hussar.example.mes.md.domain.MdWorkstation;
import com.jxdinfo.hussar.example.mes.md.domain.MdWorkstationLine;
import com.jxdinfo.hussar.example.mes.md.service.IMdWorkshopService;
import com.jxdinfo.hussar.example.mes.md.service.IMdWorkstationLineService;
import com.jxdinfo.hussar.example.mes.md.service.IMdWorkstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 产线管理Controller
 *
 * @author 常宝坤
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/md/line")
public class MdWorkstationLineController extends BaseController
{
    @Autowired
    private IMdWorkstationLineService mdWorkstationLineService;

    @Autowired
    private IMdWorkshopService mdWorkshopService;

    @Autowired
    private IMdWorkstationService mdWorkstationService;

    /**
     * 查询产线管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MdWorkstationLine mdWorkstationLine)
    {
        startPage();
        List<MdWorkstationLine> list = mdWorkstationLineService.selectMdWorkstationLineList(mdWorkstationLine);
        return getDataTable(list);
    }
    /**
     * workstationId
     */
    @GetMapping("/select")
    public AjaxResult select(MdWorkstationLine mdWorkstationLine)
    {
        List<MdWorkstationLine> list = mdWorkstationLineService.selectMdWorkstationLineList(mdWorkstationLine);
        return AjaxResult.success(list);
    }

    /**
     * 导出产线管理列表
     */
    @Log(title = "产线管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdWorkstationLine mdWorkstationLine)
    {
        List<MdWorkstationLine> list = mdWorkstationLineService.selectMdWorkstationLineList(mdWorkstationLine);
        ExcelUtil<MdWorkstationLine> util = new ExcelUtil<MdWorkstationLine>(MdWorkstationLine.class);
        util.exportExcel(response, list, "产线管理数据");
    }

    /**
     * 获取产线管理详细信息
     */
    @GetMapping(value = "/get/{lineId}")
    public AjaxResult getInfo(@PathVariable("lineId") Long lineId)
    {
        return AjaxResult.success(mdWorkstationLineService.selectMdWorkstationLineByLineId(lineId));
    }

    /**
     * 新增产线管理
     */
    @Log(title = "产线管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MdWorkstationLine mdWorkstationLine)
    {
        MdWorkshop mdWorkshop = mdWorkshopService.selectMdWorkshopByWorkshopId(mdWorkstationLine.getWorkshopId());
        if(StringUtils.isNotNull(mdWorkshop)){
            mdWorkstationLine.setWorkshopCode(mdWorkshop.getWorkshopCode());
            mdWorkstationLine.setWorkshopName(mdWorkshop.getWorkshopName());
        }else {
            return AjaxResult.error("车间不存在！");
        }

        //报工的生产任务对应的工段
        MdWorkstation workstation = mdWorkstationService.selectMdWorkstationByWorkstationId(mdWorkstationLine.getWorkstationId());
        if(StringUtils.isNotNull(workstation)){
            mdWorkstationLine.setWorkstationCode(workstation.getWorkstationCode());
            mdWorkstationLine.setWorkstationName(workstation.getWorkstationName());
        }else {
            return AjaxResult.error("当前生产任务对应的工段不存在！");
        }
        return toAjax(mdWorkstationLineService.insertMdWorkstationLine(mdWorkstationLine));
    }

    /**
     * 修改产线管理
     */
    @Log(title = "产线管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody MdWorkstationLine mdWorkstationLine)
    {
        MdWorkshop mdWorkshop = mdWorkshopService.selectMdWorkshopByWorkshopId(mdWorkstationLine.getWorkshopId());
        if(StringUtils.isNotNull(mdWorkshop)){
            mdWorkstationLine.setWorkshopCode(mdWorkshop.getWorkshopCode());
            mdWorkstationLine.setWorkshopName(mdWorkshop.getWorkshopName());
        }else {
            return AjaxResult.error("车间不存在！");
        }

        //报工的生产任务对应的工段
        MdWorkstation workstation = mdWorkstationService.selectMdWorkstationByWorkstationId(mdWorkstationLine.getWorkstationId());
        if(StringUtils.isNotNull(workstation)){
            mdWorkstationLine.setWorkstationCode(workstation.getWorkstationCode());
            mdWorkstationLine.setWorkstationName(workstation.getWorkstationName());
        }else {
            return AjaxResult.error("当前生产任务对应的工段不存在！");
        }
        return toAjax(mdWorkstationLineService.updateMdWorkstationLine(mdWorkstationLine));
    }

    /**
     * 删除产线管理
     */
    @Log(title = "产线管理", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{lineIds}")
    public AjaxResult remove(@PathVariable Long[] lineIds)
    {
        return toAjax(mdWorkstationLineService.deleteMdWorkstationLineByLineIds(lineIds));
    }
}
